package com.ruoyi.manage.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.CodeConstants;
import com.ruoyi.common.utils.ListUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.uuid.NumsUtils;
import com.ruoyi.manage.enterprise.domain.SysUserExpand;
import com.ruoyi.manage.enterprise.service.ISysUserExpandService;
import com.ruoyi.manage.organization.domain.OcFinancingMatch;
import com.ruoyi.manage.organization.mapper.OcFinancingMatchMapper;
import com.ruoyi.manage.organization.service.IOcFinancingMatchService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 需求匹配Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-19
 */
@Service
public class OcFinancingMatchServiceImpl extends ServiceImpl<OcFinancingMatchMapper, OcFinancingMatch> implements IOcFinancingMatchService {


    @Autowired
    ISysUserExpandService iSysUserExpandService;

    @Override
    public OcFinancingMatch queryGetById(Long id) {
        OcFinancingMatch ocFinancingMatch = this.getById(id);
        return ocFinancingMatch;
    }

    /**
     * 新增需求匹配
     *
     * @param ocFinancingMatch 需求匹配
     * @return 结果
     */
    @Override
    public boolean addOcFinancingMatch(OcFinancingMatch ocFinancingMatch) {

        ocFinancingMatch.setMachId(NumsUtils.generateCode(CodeConstants.APPLY_MATCH));
        boolean save = this.save(ocFinancingMatch);
        return save;
    }


    /**
     * 修改需求匹配
     *
     * @param ocFinancingMatch 需求匹配
     * @return 结果
     */
    @Override
    public boolean updateOcFinancingMatch(OcFinancingMatch ocFinancingMatch) {
        return this.updateById(ocFinancingMatch);
    }

    /**
     * 批量删除需求匹配
     *
     * @param ids 需要删除的需求匹配主键
     * @return 结果
     */
    @Override
    public boolean deleteOcFinancingMatchByIds(Long[] ids) {
        return this.removeByIds(ListUtil.toList(ids));
    }

    @Override
    public List<OcFinancingMatch> queryMatchProductByOrganization(List<SysUserExpand> expandList,String state) {

        List<String> expandPermissions = expandList.stream().map(m -> m.getDataPermission()).collect(Collectors.toList());
        List<OcFinancingMatch> list = Collections.emptyList();
        if (expandPermissions.size() > 0) {
            list = this.list(new QueryWrapper<OcFinancingMatch>().in("org_path", expandPermissions).eq("state", StrUtil.isNotBlank(state) ? state : "0"));
        }
        return list;
    }

}
